Imports System.Configuration
Imports System.Web.Configuration
Imports System.Web
Imports System.IO

Public Class ImageService
    Private Shared _provider As ImageProvider = Nothing
    Private Shared _providers As ImageCollection = Nothing
    Private Shared _lock As Object = New Object
    Private Shared isInitialized As Boolean = False
    Private Shared initializationException As Exception

    Public ReadOnly Property Provider() As ImageProvider
        Get
            Return _provider
        End Get
    End Property

    Public ReadOnly Property Providers() As ImageCollection
        Get
            Return _providers
        End Get
    End Property

    Private Shared Sub LoadProviders()
        Try
            'Get the feature's configuration info
            Dim qc As ImageConfiguration = CType(ConfigurationManager.GetSection("ImageProvider"), ImageConfiguration)
            If ((qc.DefaultProvider Is Nothing) _
                        OrElse ((qc.Providers Is Nothing) _
                        OrElse (qc.Providers.Count < 1))) Then
                Throw New System.Configuration.Provider.ProviderException(("You must specify a valid default provider."))
            End If
            _providers = New ImageCollection
            ProvidersHelper.InstantiateProviders(qc.Providers, _providers, GetType(ImageProvider))
            _providers.SetReadOnly()
            _provider = _providers(qc.DefaultProvider)
            If (_provider Is Nothing) Then
                Throw New ConfigurationErrorsException("You must specify a default provider for the feature.", qc.ElementInformation.Properties("defaultProvider").Source, qc.ElementInformation.Properties("defaultProvider").LineNumber)
            End If
        Catch ex As Exception
            initializationException = ex
            isInitialized = True
            Throw ex
        End Try
        isInitialized = True
        'error-free initialization
    End Sub

    Public Shared Function GetPhoto(ByVal photoid As String, ByVal size As Images.Enums.ImageEnumerations.PhotoSize) As Stream
        LoadProviders()
        Return _provider.GetPhoto(photoid, size)
    End Function
    Public Shared Function GetPhoto(ByVal size As Images.Enums.ImageEnumerations.PhotoSize) As Stream
        LoadProviders()
        Return _provider.GetPhoto(size)
    End Function
    Public Shared Function GetFirstPhoto(ByVal albumid As String, ByVal size As Images.Enums.ImageEnumerations.PhotoSize, ByVal PublicOnly As Boolean) As Stream
        LoadProviders()
        Return _provider.GetFirstPhoto(albumid, size, PublicOnly)
    End Function
    Public Shared Function GetPhotos(ByVal AlbumID As String, ByVal PublicOnly As Boolean) As Generic.List(Of DataTypes.Photo)
        LoadProviders()
        Return _provider.GetPhotos(AlbumID, PublicOnly)
    End Function
    Public Shared Function GetPhotos(ByVal PublicOnly As Boolean) As Generic.List(Of DataTypes.Photo)
        LoadProviders()
        Return _provider.GetPhotos(PublicOnly)
    End Function
    Public Shared Sub AddPhoto(ByVal AlbumID As String, ByVal Caption As String, ByVal BytesOriginal() As Byte, ByVal Username As String)
        LoadProviders()
        _provider.AddPhoto(AlbumID, Caption, BytesOriginal, Username)
    End Sub
    Public Shared Sub RemovePhoto(ByVal PhotoID As String, ByVal Username As String)
        LoadProviders()
        _provider.RemovePhoto(PhotoID, Username)
    End Sub
    Public Shared Sub EditPhoto(ByVal Caption As String, ByVal PhotoID As String, ByVal Username As String)
        LoadProviders()
        _provider.EditPhoto(Caption, PhotoID, Username)
    End Sub
    Public Shared Function GetAlbums(ByVal PublicOnly As Boolean) As Generic.List(Of DataTypes.Album)
        LoadProviders()
        Return _provider.GetAlbums(PublicOnly)
    End Function
    Public Shared Sub AddAlbum(ByVal Caption As String, ByVal IsPublic As Boolean, ByVal Username As String)
        LoadProviders()
        _provider.AddAlbum(Caption, IsPublic, Username)
    End Sub
    Public Shared Sub RemoveAlbum(ByVal AlbumID As String, ByVal Username As String)
        LoadProviders()
        _provider.RemoveAlbum(AlbumID, Username)
    End Sub
    Public Shared Sub EditAlbum(ByVal Caption As String, ByVal IsPublic As Boolean, ByVal AlbumID As String, ByVal Username As String)
        LoadProviders()
        _provider.EditAlbum(Caption, IsPublic, AlbumID, Username)
    End Sub

    Public Shared Function GetRandomPhoto() As Generic.List(Of DataTypes.Photo)
        LoadProviders()
        Return _provider.GetRandomPhoto
    End Function
End Class
